home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 March / macformat-035.iso / Shareware City / Developers / ICAppSourceKit1.2 / ICEvents.p < prev    next >
Encoding:
Text File  |  1995-11-07  |  7.0 KB  |  318 lines  |  [TEXT/CWIE]

  1. unit ICEvents;
  2.  
  3. interface
  4.  
  5.     const
  6.         M_Apple = 128;
  7.  
  8.     procedure HandleEvents;
  9.  
  10. implementation
  11.  
  12.     uses
  13.         EPPC, ToolUtils, AppleEvents, DiskInit, Windows, Devices, 
  14.         ICGlobals, ICDocument, ICWindows, ICMiscSubs, ICInstall, ICWindowGlobals;
  15.  
  16.     procedure DoDiskEvent (message: longInt);
  17.         var
  18.             pt: point;
  19.             oe: OSErr;
  20.     begin
  21.         if (HiWord(message) <> noErr) then begin
  22.             pt.h := ((qd.screenbits.bounds.Right - qd.screenbits.bounds.Left - 304) div 2);
  23.             pt.v := ((qd.screenbits.bounds.Bottom - qd.screenbits.bounds.Top - 156) div 3);
  24.             InitCursor;
  25.             oe := DIBadMount(pt, message);
  26.         end;
  27.     end;
  28.  
  29.     procedure DoActivateDeactivate (window: WindowPtr; activate: boolean);
  30.     begin
  31.         if activate then begin
  32.             SelectWindow(window);
  33.         end; (* if *)
  34.         WindowActivateDeactivate(window, activate);
  35.     end;
  36.  
  37.     procedure DoSuspendResume (resume: boolean);
  38.     begin
  39.         in_foreground := resume;
  40.         if FrontWindow <> nil then begin
  41.             DoActivateDeactivate(FrontWindow, resume);
  42.         end;
  43.         InitCursor;
  44.     end;
  45.  
  46.     procedure DoUpdate (window: WindowPtr);
  47.     begin
  48.         BeginUpdate(window);
  49. (* *)
  50.         EndUpdate(window);
  51.     end;
  52.  
  53.     procedure DoKey (er: EventRecord);
  54.     begin
  55.         WindowsDoKey(er);
  56.     end;
  57.  
  58.     procedure DoAutoKey (er: EventRecord);
  59.     begin
  60.         DoKey(er);
  61.     end;
  62.  
  63.     procedure AdjustMenus;
  64.     begin
  65.         WindowsAdjustMenus;
  66.     end; (* AdjustMenus *)
  67.  
  68.     procedure AdjustAllMenus;
  69.     begin
  70.         AdjustMenus;
  71.         SetItemEnable(GetMenuHandle(M_File), FM_Close, IsDocOpen or (GetWindowType(FrontWindow) = WT_About));
  72.         SetItemEnable(GetMenuHandle(M_File), FM_Save, IsDocDirty or IsDocNew);
  73.         SetItemEnable(GetMenuHandle(M_File), FM_SaveAs, IsDocOpen);
  74.         AdjustInstalMenu(M_Install);
  75.         if IsDocOpen then begin
  76.             SetMenuItemText(GetMenuHandle(M_Windows), 1, GetDocumentName);
  77.         end else begin
  78.             SetMenuItemText(GetMenuHandle(M_Windows), 1, GetAString(128, 15));
  79.         end; (* if *)
  80.     end;
  81.  
  82.     procedure DoIdle;
  83.     begin
  84.         WindowsIdle;
  85.     end;
  86.  
  87.     procedure DoMenu (menu: integer; item: integer);
  88.         var
  89.             DAName: Str255;
  90.             junk: OSErr;
  91.     begin
  92.         case menu of
  93.             M_Apple:  begin
  94.                 case item of
  95.                     1: 
  96.                         DisplayError(acOpenWindow, WindowsOpen(WT_About));
  97.                     2: 
  98.                         ;
  99.                     otherwise begin
  100.                         GetMenuItemText(GetMenuHandle(M_apple), item, DAName);
  101.                         junk := OpenDeskAcc(DAName);
  102.                     end
  103.                 end; (* case *)
  104.             end;
  105.             M_File:  begin
  106.                 case item of
  107.                     FM_New:  begin
  108.                         DisplayError(acNewDocument, DoNewDoc);
  109.                     end;
  110.                     FM_Open: 
  111.                         DisplayError(acOpenDocument, DoSFOpen);
  112.                     FM_OpenInternetPreferences: 
  113.                         DisplayError(acOpenDocument, DoOpenInternetPreferences);
  114.                     FM_Close: 
  115.                         DisplayError(acCloseWindow, DoCloseDocWindow(FrontWindow));
  116.                     FM_Save: 
  117.                         DisplayError(acSave, DoSave);
  118.                     FM_SaveAs: 
  119.                         DisplayError(acSave, DoSaveAs);
  120.                     FM_Quit: 
  121.                         DisplayError(acQuit, DoQuit);
  122.                     otherwise
  123.                         ;
  124.                 end; (* case *)
  125.             end;
  126.             M_Edit:  begin
  127.                 case item of
  128.                     EM_Undo, EM_Cut, EM_Copy, EM_Paste, EM_Clear, EM_SelectAll:  begin
  129.                         WindowsDoEditMenu(item);
  130.                     end;
  131.                     otherwise
  132.                         ;
  133.                 end;
  134.             end;
  135.             M_Install:  begin
  136.                 DoInstallMenu(menu, item);
  137.             end;
  138.             M_Windows:  begin
  139.                 DisplayError(acOpenWindow, WindowsOpen(WindowType(ord(WT_Main) + item - 1)));
  140.             end;
  141.             otherwise
  142.                 ;
  143.         end; (* case *)
  144.         if not quitNow then begin
  145.             HiliteMenu(0);
  146.         end;
  147.     end; (* DoMenu *)
  148.  
  149.     procedure DoMainClick (er: eventRecord; wp: windowPtr; code: integer);
  150.         var
  151.             mResult: longInt;
  152.             needsselect: boolean;
  153.             tempRect: Rect;
  154.     begin
  155.         needsselect := (wp <> nil) & (wp <> FrontWindow);
  156.         if needsselect & not (code in [inDrag, inContent]) then begin
  157.             SelectWindow(wp);
  158.         end;
  159.         case code of
  160.             inMenuBar:  begin
  161.                 AdjustAllMenus;
  162.                 mResult := MenuSelect(er.where);
  163.                 if mResult <> 0 then begin
  164.                     DoMenu(HiWord(mResult), LoWord(mResult));
  165.                 end; (* if *)
  166.                 if not quitNow then begin
  167.                     HiliteMenu(0);
  168.                 end; (* if *)
  169.             end;
  170.             InDrag:  begin
  171.                 if needsselect and (BAND(er.modifiers, cmdKey) = 0) then begin
  172.                     SelectWindow(wp);
  173.                 end; (* if *)
  174.                 SetPort(wp);
  175.                 tempRect := GetGrayRgn^^.rgnBBox;
  176.                 DragWindow(wp, er.where, tempRect);
  177.             end;
  178.             inGrow: 
  179.                 ;
  180.             inZoomIn, inZoomOut: 
  181.                 ;
  182.             inGoAway:  begin
  183.                 if TrackGoAway(wp, er.where) then begin
  184.                     DisplayError(acCloseWindow, DoCloseDocWindow(wp));
  185.                 end;
  186.             end;
  187.             inContent:  begin
  188.                 if needsselect then begin
  189.                     SelectWindow(wp);
  190.                 end;
  191.             end;
  192.             inSysWindow: 
  193.                 SystemClick(er, wp);
  194.             otherwise
  195.  
  196.         end;
  197.     end;
  198.  
  199.     procedure DimMenuTitles;
  200.         var
  201.             mh: MenuHandle;
  202.             flags: longint;
  203.             i: integer;
  204.             old_enable_title: boolean;
  205.             enable_title: boolean;
  206.             menu_bar_redraw: boolean;
  207.     begin
  208.         AdjustMenus;
  209.         (* edit *)
  210.         mh := GetMenuHandle(M_Edit);
  211.         flags := mh^^.enableFlags;
  212.         old_enable_title := TPbtst(flags, 0);
  213.         enable_title := false;
  214.         for i := 1 to CountMItems(mh) do begin
  215.             if TPbtst(flags, i) then begin
  216.                 enable_title := true;
  217.                 leave;
  218.             end; (* if *)
  219.         end; (* for *)
  220.         SetItemEnable(mh, 0, enable_title);
  221.         menu_bar_redraw := (enable_title <> old_enable_title);
  222.         (* windows *)
  223.         mh := GetMenuHandle(M_Windows);
  224.         flags := mh^^.enableFlags;
  225.         old_enable_title := TPbtst(flags, 0);
  226.         enable_title := IsDocOpen;
  227.         SetItemEnable(mh, 0, enable_title);
  228.         menu_bar_redraw := menu_bar_redraw or (enable_title <> old_enable_title);
  229.         (*  *)
  230.         if menu_bar_redraw then begin
  231.             DrawMenuBar;
  232.         end; (* if *)
  233.     end; (* DimMenuTitles *)
  234.  
  235.     procedure HandleEvents;
  236.         var
  237.             junkbool: boolean;
  238.             er: EventRecord;
  239.             code: integer;
  240.             wp: WindowPtr;
  241.             ch: char;
  242.             mResult: longint;
  243.             junk: OSErr;
  244.             b: boolean;
  245.             item: integer;
  246.     begin
  247.         DimMenuTitles;
  248.         junkbool := WaitNextEvent(everyEvent, er, 10, nil);
  249.         if er.what = mouseDown then begin
  250.             er := er;
  251.         end;
  252.         ch := chr(BAND(er.message, CharCodeMask));
  253.         DoIdle;
  254.         b := true;
  255.         if WindowsEarlyHandleEvent(er) then begin
  256.             b := false;
  257.         end;
  258.         if b & ((er.what = keyDown) | (er.what = autoKey)) then begin
  259.             if BAND(er.modifiers, CmdKey) <> 0 then begin
  260.                 AdjustAllMenus;
  261.                 mResult := MenuKey(ch);
  262.                 if mResult <> 0 then begin
  263.                     DoMenu(HiWord(mResult), LoWord(mResult));
  264.                     b := false;
  265.                 end;
  266.             end;
  267.             if b & WindowsEarlyHandleKey(er) then begin
  268.                 b := false;
  269.             end;
  270.         end;
  271.         if b & IsDialogEvent(er) then begin
  272.             if DialogSelect(er, wp, item) then begin
  273.                 WindowItemWhere(wp, er, item);
  274.                 b := false;
  275.             end;
  276.         end;
  277.         if b then begin
  278.             case er.what of
  279.                 MouseDown:  begin
  280.                     code := FindWindow(er.where, wp);
  281.                     if wp = nil then begin
  282.                         wp := FrontWindow;
  283.                     end;
  284.                     DoMainClick(er, wp, code);
  285.                 end;
  286.  
  287.                 KeyDown:  begin
  288.                     DoKey(er);
  289.                 end;
  290.  
  291.                 AutoKey: 
  292.                     DoAutoKey(er);
  293.  
  294.                 UpdateEvt: 
  295.                     DoUpdate(windowPtr(er.message));
  296.  
  297.                 ActivateEvt: 
  298.                     DoActivateDeactivate(windowPtr(er.message), odd(er.modifiers));
  299.  
  300.                 kOSEvent: 
  301.                     if BAND(BROTL(er.message, 8), $FF) = kSuspendResumeMessage then begin
  302.                         DoSuspendResume(BAnd(er.message, kResumeMask) <> 0);
  303.                     end;
  304.  
  305.                 DiskEvt: 
  306.                     DoDiskEvent(er.message);
  307.  
  308.                 kHighLevelEvent: 
  309.                     if has_AppleEvents then begin
  310.                         junk := AEProcessAppleEvent(er);
  311.                     end; (* if *)
  312.                 otherwise
  313.              (* do nothing *)
  314.             end; (* case *)
  315.         end;
  316.     end; (* HandleEvents *)
  317.  
  318. end. (* ICEvents *)